package com.xxxcompany.wordbook.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.xxxcompany.wordbook.dao.IWordDAO;
import com.xxxcompany.wordbook.entity.User;
import com.xxxcompany.wordbook.entity.Word;
import com.xxxcompany.wordbook.service.IWordService;
import com.xxxcompany.wordbook.service.WordServiceImpl;

@WebServlet("/user_index")
public class UserIndexServlet extends HttpServlet {
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setContentType("text/html;charset=utf-8");
		PrintWriter pw = resp.getWriter();
		
		
		// 1. 
		HttpSession session = req.getSession();
		User user = (User)session.getAttribute("user");
		
		if(user==null)
		{
			resp.sendRedirect("login");
			return;
		}
		
		// 2. 
		
		// 查询用户的单词
		IWordService wordService = new WordServiceImpl();
		List<Word> words = wordService.findAll(user.getId());
		req.setAttribute("words", words);
		
		
		// 3. 
		req.getRequestDispatcher("/WEB-INF/user_index.jsp").forward(req, resp);
		
		
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doGet(req, resp);
	}
}